/**
 * 加载脚本数据
 */
export async function load_script_data() {
    // 事件处理
    let list = JSON.parse(_global_.script_list_files("events/"));
    for (let index = 0; index < list.length; index++) {
        let path = list[index];
        let number = path.replace("events/", "").replace(".js", "");
        console.info(`加载事件处理 [${number}] , ${path} `)
        let handler_script = await import(path);
        if (handler_script) {
            _MessageHandlerData.set(Number(number), handler_script);
        }
    }
    // http处理
    let http_list = JSON.parse(_global_.script_list_files("http/"));
    for (let index = 0; index < http_list.length; index++) {
        let path = http_list[index];
        let code = path.replace("http/", "").replace(".js", "");
        console.info(`加载http处理 [${code}] , ${path} `)
        let handler_script = await import(path);
        if (handler_script) {
            _HttpHandlerData.set(code, handler_script);
        }
    }
    // 道具处理
    let item_script_cache = new Map();
    let item_handler_enums = G.ItemEnums.default;
    for (const key in item_handler_enums) {
        let path = item_handler_enums[key];
        if (path !== "items/run/xxx.js") {
            if (item_script_cache.has(path)) {
                let handler_script = item_script_cache.get(path);
                console.info(`加载道具 [${key}] , ${path} `)
                _ItemHandlerData.set(key, handler_script);
            } else {
                let handler_script = await import(item_handler_enums[key]);
                console.info(`加载道具 [${key}] , ${path} `)
                _ItemHandlerData.set(key, handler_script);
                item_script_cache.set(item_handler_enums[key], handler_script);
            }
        }
    }
    item_script_cache.clear();
    //加载技能数据
    let skill_script_list = JSON.parse(_global_.script_list_files("skills"));
    for (let index = 0; index < skill_script_list.length; index++) {
        let path = skill_script_list[index];
        let skill_script = await import(path);
        let skill_name = skill_script.default["名称"];
        let skill_type = skill_script.default["技能类型"] || "其他";
        console.info(`加载技能 [${skill_name}] [${skill_type}] , ${path} `)
        _SkillData.set(skill_name, skill_script.default);
    }
    //加载灵宝数据
    let lb_script_list = JSON.parse(_global_.script_list_files("lbs"));
    for (let index = 0; index < lb_script_list.length; index++) {
        let path = lb_script_list[index];
        let lb_script = await import(path);
        let lb_name = lb_script.default["名称"];
        console.info(`加载灵宝 [${lb_name}] , ${path} `)
        _LingBaoData.set(lb_name, lb_script.default);
    }
    //加载法宝数据
    let fb_script_list = JSON.parse(_global_.script_list_files("fbs"));
    for (let index = 0; index < fb_script_list.length; index++) {
        let path = fb_script_list[index];
        let fb_script = await import(path);
        let fb_name = fb_script.default["名称"];
        console.info(`加载法宝 [${fb_name}] , ${path} `)
        _LingBaoData.set(fb_name, fb_script.default);
    }
    //加载地图坐标数据
    let map_coordinate_script_list = JSON.parse(_global_.script_list_files("map_coordinate"));
    for (let index = 0; index < map_coordinate_script_list.length; index++) {
        let path = map_coordinate_script_list[index];
        let map_coordinate_script = await import(path);
        let number = path.replace("map_coordinate/", "").replace(".js", "");
        console.info(`加载地图坐标数据 [${number}] , ${path} `)
        _MapCoordinateData.set(Number(number), map_coordinate_script.default);
    }
    //加载对话数据
    let dialogue_script_list = JSON.parse(_global_.script_list_files("dialogue"));
    for (let index = 0; index < dialogue_script_list.length; index++) {
        let path = dialogue_script_list[index];
        let dialogue_script = await import(path);
        let number = path.replace("dialogue/", "").replace(".js", "");
        console.info(`加载地图NPC对话数据 [${number}] , ${path} `)
        _DialogueData.set(number, dialogue_script.default);
    }
    //加载对话处理
    let dialogue_handler_script_list = JSON.parse(_global_.script_list_files("dialogue_handler"));
    for (let index = 0; index < dialogue_handler_script_list.length; index++) {
        let path = dialogue_handler_script_list[index];
        let dialogue_handler_script = await import(path);
        let number = path.replace("dialogue_handler/", "").replace(".js", "");
        console.info(`加载地图NPC对话处理 [${number}] , ${path} `)
        _DialogueHandlerData.set(number, dialogue_handler_script.default);
    }
    //加载战斗结算数据
    let fight_settlement_script_list = JSON.parse(_global_.script_list_files("fight_settlement"));
    for (let index = 0; index < fight_settlement_script_list.length; index++) {
        let path = fight_settlement_script_list[index];
        let fight_settlement_script = await import(path);
        let number = fight_settlement_script.default["编号"];
        console.info(`加载战斗结算数据 [${number}] , ${path} `)
        _FightSettlementData.set(number, fight_settlement_script.default);
    }
    //加载副本数据
    let dungeon_script_list = JSON.parse(_global_.script_list_files("dungeon"));
    for (let index = 0; index < dungeon_script_list.length; index++) {
        let path = dungeon_script_list[index];
        let dungeon_script = await import(path);
        let dungeon_name = dungeon_script.default["名称"];
        let number = path.replace("dungeon/", "").replace(".js", "");
        console.info(`加载副本 [${dungeon_name}] , ${path} `)
        _DungeonData.set(Number(number), dungeon_script.default);
        if (dungeon_script.default["对话"]) {
            for (const key in dungeon_script.default["对话"]) {
                let dungeon_map_npc_dialogue = dungeon_script.default["对话"][key];
                console.info(`加载副本地图NPC对话数据, [${dungeon_name}] 地图: [${key}] , NPC: [${Object.keys(dungeon_map_npc_dialogue)}]`)
                _DialogueData.set(`${number}_${key}`, dungeon_map_npc_dialogue);
            }
        }
        if (dungeon_script.default["对话处理"]) {
            for (const key in dungeon_script.default["对话处理"]) {
                let dungeon_map_npc_handler = dungeon_script.default["对话处理"][key];
                console.info(`加载副本地图NPC对话处理, [${dungeon_name}] 地图: [${key}] , NPC: [${Object.keys(dungeon_map_npc_handler)}]`)
                _DialogueHandlerData.set(`${number}_${key}`, dungeon_map_npc_handler);
            }
        }
        if (dungeon_script.default["战斗结算"]) {
            for (const key in dungeon_script.default["战斗结算"]) {
                let settlement = dungeon_script.default["战斗结算"][key];
                console.info(`加载副本地图战斗结算数据, [${dungeon_name}] 编号: [${key}]`)
                _FightSettlementData.set(key, settlement);
            }
        }
    }
    // 加载物品掉落
    let drops_items_script_list = JSON.parse(_global_.script_list_files("drops_items"));
    for (let index = 0; index < drops_items_script_list.length; index++) {
        let path = drops_items_script_list[index];
        let drops_items_script = await import(path);
        let number = drops_items_script.default["编号"];
        let desc = drops_items_script.default["说明"];
        console.info(`加载物品掉落数据 [${desc}] 编号: [${number}] , ${path} `)
        _DropsItemsData.set(number, drops_items_script.default["函数"]);
    }
}

/**
 * 重载事件处理脚本
 * @param {*} path 
 */
export async function reload_script_data(path) {
    if (path.startsWith("events/")) {
        let number = path.replace("events/", "").replace(".js", "");
        let handler_script = await import(path);
        console.info(`重载事件处理 [${number}] , ${path} `)
        _MessageHandlerData.set(Number(number), handler_script);
    }
    if (path.startsWith("http/")) {
        let code = path.replace("http/", "").replace(".js", "");
        let handler_script = await import(path);
        console.info(`重载http处理 [${code}] , ${path} `)
        _HttpHandlerData.set(code, handler_script);
    }
    if (path.startsWith("items/")) {
        let item_handler_enums = G.ItemEnums.default;
        let handler_script = undefined;
        for (const key in item_handler_enums) {
            if(item_handler_enums[key] === path){
                if(handler_script === undefined){
                    handler_script = await import(path);
                }
                _ItemHandlerData.set(key, handler_script);
                console.info(`重载道具脚本 [${key}] ${path}`);
            }
        }
    }
    if (path.startsWith("skills/")) {
        let skill_script = await import(path);
        let skill_name = skill_script.default["名称"];
        console.info(`重载技能数据 [${skill_name}] , ${path} `)
        _SkillData.set(skill_name, skill_script.default);
    }
    if (path.startsWith("lbs/")) {
        let lb_script = await import(path);
        let lb_name = lb_script.default["名称"];
        console.info(`重载灵宝数据 [${lb_name}] , ${path} `)
        _LingBaoData.set(lb_name, lb_script.default);
    }
    if (path.startsWith("fbs/")) {
        let fb_script = await import(path);
        let fb_name = fb_script.default["名称"];
        console.info(`重载法宝数据 [${fb_name}] , ${path} `)
        _LingBaoData.set(fb_name, fb_script.default);
    }
    if (path.startsWith("dungeon/")) {
        let dungeon_script = await import(path);
        let dungeon_name = dungeon_script.default["名称"];
        console.info(`重载副本数据 [${dungeon_name}] , ${path} `)
        let number = path.replace("dungeon/", "").replace(".js", "");
        _DungeonData.set(Number(number), dungeon_script.default);
    }
    if (path.startsWith("map_coordinate/")) {
        let map_coordinate_script = await import(path);
        let number = path.replace("map_coordinate/", "").replace(".js", "");
        console.info(`重载加载地图坐标数据 [${number}] , ${path} `)
        _MapCoordinateData.set(Number(number), map_coordinate_script.default);
    }
    if (path.startsWith("dialogue/")) {
        let dialogue_script = await import(path);
        let number = path.replace("dialogue/", "").replace(".js", "");
        console.info(`重载地图NPC对话数据  [${number}] , ${path} `)
        _DialogueData.set(number, dialogue_script.default);
    }
    if (path.startsWith("dialogue_handler/")) {
        let dialogue_handler_script = await import(path);
        let number = path.replace("dialogue_handler/", "").replace(".js", "");
        console.info(`重载地图NPC对话处理  [${number}] , ${path} `)
        _DialogueHandlerData.set(number, dialogue_handler_script.default);
    }
    if (path.startsWith("dungeon/")) {
        let dungeon_script = await import(path);
        let dungeon_name = dungeon_script.default["名称"];
        let number = path.replace("dungeon/", "").replace(".js", "");
        console.info(`重载副本数据  [${dungeon_name}] , ${path} `)
        _DungeonData.set(Number(number), dungeon_script.default);
        if (dungeon_script.default["对话"]) {
            for (const key in dungeon_script.default["对话"]) {
                let dungeon_map_npc_dialogue = dungeon_script.default["对话"][key];
                console.info(`重载副本地图NPC对话数据, [${dungeon_name}] 地图: [${key}] , NPC: [${Object.keys(dungeon_map_npc_dialogue)}]`)
                _DialogueData.set(`${number}_${key}`, dungeon_map_npc_dialogue);
            }
        }
        if (dungeon_script.default["对话处理"]) {
            for (const key in dungeon_script.default["对话处理"]) {
                let dungeon_map_npc_handler = dungeon_script.default["对话处理"][key];
                console.info(`重载副本地图NPC对话处理, [${dungeon_name}] 地图: [${key}] , NPC: [${Object.keys(dungeon_map_npc_handler)}]`)
                _DialogueHandlerData.set(`${number}_${key}`, dungeon_map_npc_handler);
            }
        }
        if (dungeon_script.default["战斗结算"]) {
            for (const key in dungeon_script.default["战斗结算"]) {
                let settlement = dungeon_script.default["战斗结算"][key];
                console.info(`重载副本地图战斗结算, [${dungeon_name}] 编号: [${key}]`)
                _FightSettlementData.set(key, settlement);
            }
        }
    }
    if (path.startsWith("fight_settlement/")) {
        let fight_settlement_script = await import(path);
        let number = fight_settlement_script.default["编号"];
        console.info(`重载战斗结算数据 [${number}] , ${path} `)
        _FightSettlementData.set(number, fight_settlement_script.default);
    }
    if (path.startsWith("drops_items/")) {
        let drops_items_script = await import(path);
        let number = drops_items_script.default["编号"];
        let desc = drops_items_script.default["说明"];
        console.info(`重载物品掉落数据 [${desc}] 编号: [${number}] , ${path} `)
        _DropsItemsData.set(number, drops_items_script.default["函数"]);
    }
}

